import 'package:film/model/film_model.dart';
import 'package:film/service/api_service.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class FilmRankController extends GetxController with GetSingleTickerProviderStateMixin {
  final DioService dioService = DioService();
  late final TabController tabController;
  final List<String> filmTypeList = ['番剧', '剧场版', '4K', '待添加'];
  final rankFilmList = <Film>[].obs;

  @override
  void onInit() {
    super.onInit();
    // 初始化TabController
    tabController = TabController(
      length: filmTypeList.length,
      vsync: this,
    );
    // 为TabController添加监听器，当Tab切换时触发
    tabController.addListener(() {
      // 确保是在Tab切换动画结束后才执行，防止重复调用
      if (!tabController.indexIsChanging) {
        final index = tabController.index;
        getRankFilmList((index + 1).toString());
      }
    });
    getRankFilmList('1');
  }

  /// 获取数据
  void getRankFilmList(String selectfilmType) async {
    SearchCriteria criteria = SearchCriteria(
      page: "1",
      limit: "3",
      resultType: "1",
      typeId: selectfilmType,
    );
    ApiResponse<List<Film>> response = await dioService.searchVideo(criteria);
    if (response.success) {
      rankFilmList.assignAll(response.data ?? []);
    }
  }

  @override
  void onClose() {
    tabController.dispose();
    super.onClose();
  }
}
